<?php

use Phalcon\Mvc\Model;

class Order extends Model {

    public $order_id;
    public $order_amount;
    public $serial_number;
    public $order_time;
    public $send_time;
    public $leword;
    public $status;
    public $express_code;
    public $express_com;
    public $client_id;
    public $product_count;
    public $address_hash;
    public $order_dixian;
    public $wepay_method;
    /**
     * 订单使用的积分
     * @var
     */
    public $order_credit;

    /**
     * 订单赠送的积分
     * @var
     */
    public $order_credit_add;

    public function initialize() {
        $this->setSource("orders");
    }

    /**
     * 获取订单详情
     * @param type $orderId
     */
    public static function getOrderDetails($orderId) {
        global $config;
        $order_details = OrderDetail::find([
            "order_id = $orderId",
            'columns' => 'product_id, product_discount_price AS sell_price, product_count'
        ])->toArray();
        // 加工数据
        foreach ($order_details as &$value) {
            $value['product_id'] = intval($value['product_id']);
            $value['product_count'] = intval($value['product_count']);
            $value['sell_price'] = floatval($value['sell_price']);
            $product = Product::findFirst([
                "product_id = {$value['product_id']}",
                "columns" => "product_name,catimg AS product_image"
            ]);
            if ($product) {
                // 商品图片
                $product->product_image = $config->base->domainbase . 'uploads/product_hpic/' . $product->product_image;
                $value['product_name'] = $product->product_name;
                $value['product_image'] = $product->product_image;
            } else {
                $value['product_name'] = null;
                $value['product_image'] = null;
            }
        }
        return $order_details;
    }

    /**
     * 拉取地址信息
     * @param type $orderId
     * @return type
     */
    public static function getAddressInfo($orderId) {
        // 拉取地址信息
        $order_address = OrderAddress::findFirst([
            "order_id = $orderId",
            'columns' => 'client_id AS uid, user_name AS uname, tel_number AS phone, province, \'\' AS dist, city, postal_code AS poscode, address AS addrs'
        ]);
        if ($order_address) {
            $order_address->uid = intval($order_address->uid);
            return $order_address->toArray();
        } else {
            return [];
        }
    }

    /**
     * 获取快递信息
     * @param type $company_code
     */
    public static function getExpressCompany($company_code) {
        $expcode = include APP_PATH . 'app/config/express_code.php';
        if ($company_code != '') {
            return $expcode[$company_code];
        } else {
            return NULL;
        }
    }

    /**
     * 计算订单总额
     * @param $orderId
     */
    public static function getOrderTotalFee($orderId) {
        $order = Order::findFirst("order_id = $orderId");
        if ($order) {
            return floatval($order->order_amount);
        } else {
            return 0;
        }
    }

    /**
     * 扣除库存数据
     * @param $orderId
     * @param $isUp
     */
    public static function cutProductStock($orderId, $isUp = true) {
        $order_details = OrderDetail::find([
            "order_id = $orderId",
            'columns' => 'product_id, product_count'
        ])->toArray();
        // 加工数据
        foreach ($order_details as $value) {
            try {
                $product = Product::findFirst("product_id = $value[product_id]");
                if ($isUp) {
                    // 减库存
                    $product->product_instocks = $product->product_instocks - $value['product_count'];
                } else {
                    // 加库存
                    $product->product_instocks = $product->product_instocks + $value['product_count'];
                }
                $product->save();
            } catch (Exception $ex) {
                continue;
            }
        }
    }

    /**
     * 计算用户积分
     * @param $orderId
     * @param $uid
     */
    public static function updateUserCredit($orderId, $uid) {
        $orderInfo = Order::findFirst("order_id = '$orderId'");
        $user = User::findFirst("client_id = $uid");
        $creditRate = floatval(Settings::get('credit_order_amount'));
        if ($creditRate > 0) {
            $creditAdd = $orderInfo->order_amount * $creditRate;
            try {
                CreditRecord::log($uid, $creditAdd, 0, $orderId, '订单赠送积分');
                $user->client_credit = $user->client_credit + $creditAdd;
                return $user->save();
            } catch (Exception $ex) {
                return false;
            }
        } else {
            return false;
        }
    }

}
